package com.ruoyi.web.controller.his;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.his.domain.MedicalOrder;
import com.ruoyi.his.service.IMedicalOrderService;
import org.springframework.core.annotation.Order;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.his.domain.Bed;
import com.ruoyi.his.service.IBedService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 病床信息Controller
 * 
 * @author hzr
 * @date 2024-10-19
 */
@RestController
@RequestMapping("/his/bed")
public class BedController extends BaseController
{
    @Autowired
    private IBedService bedService;

    @Autowired
    private IMedicalOrderService medicalOrderService;
    /**
     * 查询病床信息列表
     */
    @PreAuthorize("@ss.hasPermi('his:bed:list')")
    @GetMapping("/list")
    public TableDataInfo list(Bed bed)
    {
        startPage();
        List<Bed> list = bedService.selectBedList(bed);
        return getDataTable(list);
    }

    /**
     * 导出病床信息列表
     */
    @PreAuthorize("@ss.hasPermi('his:bed:export')")
    @Log(title = "病床信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bed bed)
    {
        List<Bed> list = bedService.selectBedList(bed);
        ExcelUtil<Bed> util = new ExcelUtil<Bed>(Bed.class);
        util.exportExcel(response, list, "病床信息数据");
    }

    /**
     * 获取病床信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:bed:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bedService.selectBedById(id));
    }

    /**
     * 新增病床信息
     */
    @PreAuthorize("@ss.hasPermi('his:bed:add')")
    @Log(title = "病床信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bed bed)
    {
        return toAjax(bedService.insertBed(bed));
    }


    @PostMapping("/batchAdd")
    public AjaxResult batchAdd(@RequestBody Integer count) {
        try {
            bedService.batchAddBeds(count);
            return AjaxResult.success("病床批量添加成功");
        } catch (Exception e) {
            return AjaxResult.error("批量添加病床失败", e.getMessage());
        }
    }

    @GetMapping("/availableBeds")
    public AjaxResult getAvailableBeds() {
        try {
            List<Bed> availableBeds = bedService.getAvailableBeds();
//            System.out.println(availableBeds);
            return AjaxResult.success(availableBeds);
        } catch (Exception e) {
            return AjaxResult.error("获取空闲病床失败：" + e.getMessage());
        }
    }


    @PreAuthorize("@ss.hasPermi('his:bed:edit')")
    @Log(title = "病床信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bed bed)
    {
        return toAjax(bedService.updateBed(bed));
    }

    /**
     * 删除病床信息
     */
    @PreAuthorize("@ss.hasPermi('his:bed:remove')")
    @Log(title = "病床信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bedService.deleteBedByIds(ids));
    }
}
